package com.xxx.kjds.controller;

import com.xxx.kjds.entity.User;
import com.xxx.kjds.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    UserService userService;

    @RequestMapping("/userRegister")
    @ResponseBody
    public String register(HttpServletRequest request) {
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String account_type = request.getParameter("account_type");
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setAccountType(account_type);
        user.setPhone(phone);
        userService.insert(user);
        return "success";
    }

    @RequestMapping("/fail")
    public String fail() {
        return "fail";
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public String checkName(HttpServletRequest request) {
        String username = request.getParameter("check_username");
        User user = userService.checkName(username);
        if (user != null) {
            return "该用户已被注册!";
        } else {
            return "该用户可注册!";
        }

    }
}
